
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.adapter.RechargeModeAdapter;
import com.kugou.game.sdk.adapter.RechargeModeAdapter.ModeInfo;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.PayManager;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.PayConstant;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 描述:充值模式选择界面
 *
 * @author wcj
 * @since 2015年11月2日 上午11:40:56
 */
public class RechargeSelectModeFragment extends BaseSDKWorkerFragment {

    public static interface OnModeSelectedListener {
        public void onSeleced(int mode);
    }

    private Context mContext;

    private GridView mRechargeModeGridView;

    private RechargeModeAdapter mRechargeModeAdapter;

    private ArrayList<ModeInfo> mModeInfos = new ArrayList<RechargeModeAdapter.ModeInfo>();

    private OnModeSelectedListener mModeSelectedListener;

    private boolean mShowRecommed;

    private int mFixMoneyNum;

    private boolean mIsRechargeYoubi;

    public OnModeSelectedListener getModeSelectedListener() {
        return mModeSelectedListener;
    }

    public void setModeSelectedListener(OnModeSelectedListener modeSelectedListener) {
        this.mModeSelectedListener = modeSelectedListener;
    }

    public static RechargeSelectModeFragment getFragment(boolean showRecommond, int fixMoneyNum,
            boolean isRechargeYoubi) {
        RechargeSelectModeFragment fragment = new RechargeSelectModeFragment();
        fragment.mShowRecommed = showRecommond;
        fragment.mFixMoneyNum = fixMoneyNum;
        fragment.mIsRechargeYoubi = isRechargeYoubi;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    @Override
    public void setupActions(ArrayList<String> actions) {
        super.setupActions(actions);
        actions.add(SDKActions.ACTION_YOUBI_RATIO_CHANGE);
    }

    @Override
    public void handleBroadcast(Context context, Intent intent) {
        super.handleBroadcast(context, intent);
        String action = intent.getAction();
        if (action.equals(SDKActions.ACTION_YOUBI_RATIO_CHANGE)) {
            TextView retainYoubi = (TextView) findViewById(Rx.id.kg_recharge_youbi_retain);
            if (retainYoubi != null) {
                retainYoubi
                        .setText("可用余额 "
                                + (int) UserManager.getInstance().getCurrentUserInternal()
                                        .getKubiNum()
                                + "游币 （"
                                + (int) (UserManager.getInstance().getCurrentUserInternal()
                                        .getKubiNum() / PayManager.getYoubiRatio()) + "元）");
            }

            ImageView redRound = (ImageView) findViewById(Rx.id.kg_recharge_mode_red_round);
            if (PayManager.isShowRedPoint() && PayManager.hasYoubiRechargepreferential()) {
                if (redRound != null) {
                    redRound.setVisibility(View.VISIBLE);
                }
                if (retainYoubi != null) {
                    retainYoubi.setText(PayManager.getKubiRechargePreferentialTitle());
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
            return inflater.inflate(Rx.layout.kg_fragment_recharge_select_mode_land, null);
        }
        return inflater.inflate(Rx.layout.kg_fragment_recharge_select_mode, null);
    }

    private void initViews() {
        TextView accountTv = (TextView) findViewById(Rx.id.kg_account_tv);
        accountTv.setText(Html.fromHtml("<font color=#33333>账号：</font>" + "<font color=#479dfc>"
                + UserManager.getInstance().getCurrentUserName() + "</font>"));

        TextView youbiTv = (TextView) findViewById(Rx.id.kg_youbi_tv);
        int youbiNum = (int) UserManager.getInstance().getCurrentUserInternal().getKubiNum();
        youbiTv.setText(Html.fromHtml("<font color=#33333>游币余额：</font>" + "<font color=#479dfc>"
                + youbiNum + "游币（" + youbiNum / PayManager.getYoubiRatio() + "元）" + "</font>"));

        mRechargeModeGridView = (GridView) findViewById(Rx.id.kg_recharge_mode_gv);

        if (mShowRecommed && !mIsRechargeYoubi) {
            RelativeLayout recommedLayout = (RelativeLayout) findViewById(Rx.id.kg_recharge_recommend_layout);
            recommedLayout.setVisibility(View.VISIBLE);

            TextView retainYoubi = (TextView) findViewById(Rx.id.kg_recharge_youbi_retain);
            retainYoubi
                    .setText("可用余额 "
                            + (int) UserManager.getInstance().getCurrentUserInternal().getKubiNum()
                            + "游币 （"
                            + (int) (UserManager.getInstance().getCurrentUserInternal()
                                    .getKubiNum() / PayManager.getYoubiRatio()) + "元）");

            ImageView redRound = (ImageView) findViewById(Rx.id.kg_recharge_mode_red_round);
            if (PayManager.isShowRedPoint() && PayManager.hasYoubiRechargepreferential()) {
                redRound.setVisibility(View.VISIBLE);
                retainYoubi.setText(PayManager.getKubiRechargePreferentialTitle());
            }

            RelativeLayout recommendItemLayout = (RelativeLayout) findViewById(Rx.id.kg_recharge_recommend_item_layout);
            recommendItemLayout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mModeSelectedListener != null) {
                        mModeSelectedListener.onSeleced(PayConstant.ChargeIndexMode.MODE_YOUBI);
                    }

                }
            });
        }

        mModeInfos.clear();
        mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_alipay,
                Rx.string.kg_recharge_mode_alipay));
        mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_unoin,
                Rx.string.kg_recharge_mode_union));
        mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_weixin,
                Rx.string.kg_recharge_mode_weixin));
        if (mFixMoneyNum <= 0) {
            mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_mobilecard,
                    Rx.string.kg_recharge_mode_mobile_card));
        }
        if (!mShowRecommed && !mIsRechargeYoubi) {
            mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_youbi,
                    Rx.string.kg_recharge_mode_youbi));
        }

        if (mFixMoneyNum <= 0) {
            mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_gamecard,
                    Rx.string.kg_recharge_mode_game_card));
            mModeInfos.add(new ModeInfo(Rx.drawable.kg_img_recharge_mode_sms,
                    Rx.string.kg_recharge_mode_sms));
        }
        mRechargeModeAdapter = new RechargeModeAdapter(mModeInfos, mContext);
        mRechargeModeGridView.setAdapter(mRechargeModeAdapter);

        mRechargeModeGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mModeSelectedListener != null) {
                    if (mIsRechargeYoubi) {
                        if (position >= PayConstant.ChargeIndexMode.MODE_YOUBI) {
                            mModeSelectedListener.onSeleced(position + 1);
                        } else {
                            mModeSelectedListener.onSeleced(position);
                        }
                    } else {
                        // 普通支付，有推荐情况，游币被提到推荐模块.大于游币的必须加一
                        if (mShowRecommed && position >= PayConstant.ChargeIndexMode.MODE_YOUBI
                                && mFixMoneyNum <= 0) {
                            mModeSelectedListener.onSeleced(position + 1);
                        } else if (!mShowRecommed && mFixMoneyNum > 0 && position == 3) {
                            // 定额支付，充值卡会被隐藏，游币上升一个位置
                            mModeSelectedListener.onSeleced(PayConstant.ChargeIndexMode.MODE_YOUBI);
                        } else {
                            mModeSelectedListener.onSeleced(position);
                        }
                    }

                }
            }
        });

    }
}
